package com.pactera.redis.autoconfigure;

import org.apache.commons.collections4.CollectionUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.ClusterServersConfig;
import org.redisson.config.Config;
import org.redisson.config.SentinelServersConfig;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author: liqingdian
 **/
@Configuration
public class RedissonConfiguration {

    @Autowired
    private RedisProperties redisProperties;

    /**
     * 分布式缓存锁
     *
     * @Author: liqingdian
     */
    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
            prefix = "spring.redis.pactera.redisson",
            name = {"enabled"},
            havingValue = "true")
    public RedissonClient redissonClient() {
        Config config = new Config();
        RedisProperties.RedissonProperties redissonProperties = redisProperties.getRedisson();
        boolean cluster = redissonProperties.isCluster();
        List<String> nodes = redissonProperties.getNodes();
        if (cluster) {
            ClusterServersConfig clusterServersConfig = config.useClusterServers();
            // 设置集群状态扫描时间
            clusterServersConfig.setScanInterval(redissonProperties.getScanInterval());
            clusterServersConfig.addNodeAddress(nodes.toArray(new String[nodes.size()]));

        } else if (CollectionUtils.isNotEmpty(nodes)) {
            SentinelServersConfig sentinelServersConfig = config.useSentinelServers();
            sentinelServersConfig.setDatabase(redissonProperties.getDatabase());
            // 设置哨兵状态扫描时间
            sentinelServersConfig.setScanInterval(redissonProperties.getScanInterval());
            sentinelServersConfig.setMasterName(redissonProperties.getMasterName());
            sentinelServersConfig.addSentinelAddress(nodes.toArray(new String[nodes.size()]));
        } else {
            SingleServerConfig singleServerConfig = config.useSingleServer();
            singleServerConfig.setAddress(redissonProperties.getAddress());
            singleServerConfig.setDatabase(redissonProperties.getDatabase());
            singleServerConfig.setPassword(redissonProperties.getPassword());
            singleServerConfig.setConnectionPoolSize(redissonProperties.getConnectionPoolSize());
            singleServerConfig.setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize());
            singleServerConfig.setSubscriptionConnectionPoolSize(redissonProperties.getSubscriptionConnectionPoolSize());
            singleServerConfig.setSubscriptionConnectionMinimumIdleSize(redissonProperties.getSubscriptionConnectionMinimumIdleSize());
        }
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}
